@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">出库操作</h5>
                </div>
                <div class="card-body">
                    <form id="outForm" method="POST" action="{{ route('inventory.store-out') }}">
                        @csrf
                        <div class="mb-3">
                            <label for="material_id" class="form-label">材料</label>
                            <select name="material_id" id="material_id" class="form-select" required>
                                <option value="">请选择材料</option>
                                @foreach($materials as $material)
                                    <option value="{{ $material->id }}" 
                                        data-quantity="{{ $material->quantity }}"
                                        data-unit="{{ $material->unit }}">
                                        {{ $material->name }} (当前库存: {{ $material->quantity }} {{ $material->unit }})
                                    </option>
                                @endforeach
                            </select>
                        </div>

                        <div class="mb-3">
                            <label for="quantity" class="form-label">数量</label>
                            <input type="number" step="0.01" class="form-control" id="quantity" name="quantity" required>
                            <div class="form-text">当前库存: <span id="currentQuantity">0</span> <span id="currentUnit"></span></div>
                        </div>

                        <div class="mb-3">
                            <label for="remark" class="form-label">备注</label>
                            <textarea class="form-control" id="remark" name="remark" rows="3"></textarea>
                        </div>

                        <div class="d-flex justify-content-between">
                            <a href="{{ route('inventory.index') }}" class="btn btn-secondary">返回</a>
                            <button type="submit" class="btn btn-primary">确认出库</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 提示框 -->
<div class="modal fade" id="alertModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">提示</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <p id="alertMessage"></p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="confirmButton">确定</button>
            </div>
        </div>
    </div>
</div>

@endsection

@push('scripts')
<script>
$(document).ready(function() {
    // 更新当前库存显示
    $('#material_id').change(function() {
        var option = $(this).find('option:selected');
        var quantity = option.data('quantity');
        var unit = option.data('unit');
        $('#currentQuantity').text(quantity);
        $('#currentUnit').text(unit);
    });

    // 表单提交
    $('#outForm').submit(function(e) {
        e.preventDefault();
        
        $.ajax({
            url: $(this).attr('action'),
            method: 'POST',
            data: $(this).serialize(),
            success: function(response) {
                if (response.success) {
                    $('#alertMessage').text(response.message);
                    $('#confirmButton').show().off('click').on('click', function() {
                        window.location.href = '{{ route('inventory.index') }}';
                    });
                } else {
                    $('#alertMessage').text(response.message);
                    $('#confirmButton').hide();
                }
                $('#alertModal').modal('show');
            },
            error: function(xhr) {
                var message = '操作失败';
                if (xhr.responseJSON && xhr.responseJSON.message) {
                    message = xhr.responseJSON.message;
                }
                $('#alertMessage').text(message);
                $('#confirmButton').hide();
                $('#alertModal').modal('show');
            }
        });
    });
});
</script>
@endpush 